import http, { fetchHeader } from './http'
import { getUser } from '../auth'

export const publish = async(form) =>{
    return await http.fetchPost('/api/task/publish', form)
}

export const status = async() =>{
    let user = getUser()
    let status = { running : 0, month : 0, count : 0 }
    if(user.role == 'USER'){
        status = await http.fetchGet(`/api/task/statusByUser?username=${user.user}`)
    }else if(user.role == 'COMPANY'){
        status = await http.fetchGet(`/api/task/statusByCompany?company=${user.user}`)
    }else if(user.role == 'ADMIN'){
        status = await http.fetchGet('/api/task/status')
    }
    return [{
        count: status.running || 0,
        title: '推送中',
        desc : '正在执行中的任务数量',
    },{
        count: status.month || 0,
        title: '本月推送数量',
        desc : '本月已推送至广告商的任务数量',
    },{
        count: status.count || 0,
        title: '总推送数量',
        desc : '已推送至广告商的任务总数量',
    }]
}

export const getPage = async(index, pageSize) =>{
    let user = getUser()
    if(user.role == 'USER'){
        return await http.fetchGet(`/api/task/pageByUser?username=${user.user}&pageIndex=${index}&pageSize=${pageSize}`);
    }else if(user.role == 'COMPANY'){
        return await http.fetchGet(`/api/task/pageByCompany?company=${user.user}&pageIndex=${index}&pageSize=${pageSize}`);
    }else if(user.role == 'ADMIN'){
        return await http.fetchGet(`/api/task/page?pageIndex=${index}&pageSize=${pageSize}`);
    }
}

export const groupMonth = async(index, pageSize) =>{
    let user = getUser()
    if(user.role == 'USER'){
        return await http.fetchGet(`/api/task/groupMonthByUser?username=${user.user}`);
    }else if(user.role == 'COMPANY'){
        return await http.fetchGet(`/api/task/groupMonthByCompany?company=${user.user}`);
    }else if(user.role == 'ADMIN'){
        return await http.fetchGet(`/api/task/groupMonth`);
    }
}

export const groupChannel = async(index, pageSize) =>{
    let user = getUser()
    if(user.role == 'USER'){
        return await http.fetchGet(`/api/task/groupChannelByUser?username=${user.user}`);
    }else if(user.role == 'COMPANY'){
        return await http.fetchGet(`/api/task/groupChannelByCompany?company=${user.user}`);
    }else if(user.role == 'ADMIN'){
        return await http.fetchGet(`/api/task/groupChannel`);
    }
}

export const getHeader = fetchHeader